#include <iostream>
#include <string>
#include <cstring>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <memory>
#include "thread.hpp"

// 多线程客户端读写用的socketFd是同一个 UDP是全双工的：可以同时进行收发而不受干扰
uint16_t svrPort = 0;
std::string svrIp;

void usage(std::string proc)
{
    std::cout << std::endl
              << "Usage: " << proc << " svrIp svrPort" << std::endl
              << std::endl;
}

void *udpSender(void *threadInfo)
{
    int socketFd = *(int *)((ThreadInfo *)threadInfo)->_ptrSocketFd;
    std::string threadName = ((ThreadInfo *)threadInfo)->_threadName;

    struct sockaddr_in svr_sockAddr;
    memset(&svr_sockAddr, 0, sizeof(svr_sockAddr));
    svr_sockAddr.sin_family = AF_INET;
    svr_sockAddr.sin_addr.s_addr = inet_addr(svrIp.c_str());
    svr_sockAddr.sin_port = htons(svrPort);

    std::string msg;
    while (true)
    {
        std::cerr << "请输入你的信息# "; // for test:标准错误打印 stdin重定向到管道
        std::getline(std::cin, msg);
        if (msg == "quit")
            break;

        // 当client首次发送消息给服务器的时候，OS会自动给client bind他的IP和PORT
        sendto(socketFd, msg.c_str(), msg.size(), 0, (struct sockaddr *)&svr_sockAddr, sizeof svr_sockAddr);
    }

    return nullptr;
}

static void *udpRecvr(void *threadInfo)
{
    int socketFd = *(int *)((ThreadInfo *)threadInfo)->_ptrSocketFd;
    std::string threadName = ((ThreadInfo *)threadInfo)->_threadName;

    char buffer[1024];
    while (true)
    {
        memset(buffer, 0, sizeof(buffer));

        //发送方的地址信息
        struct sockaddr_in sender_sockAddr;
        socklen_t len = sizeof(sender_sockAddr);
        ssize_t s = recvfrom(socketFd, buffer, sizeof(buffer) - 1, 0, (struct sockaddr *)&sender_sockAddr, &len);
        if (s > 0)
        {
            buffer[s] = 0;
            std::cout << buffer << std::endl;
        }
    }
}

// ./udp_client 127.0.0.1 8080
int main(int argc, char *argv[])
{
    if (argc != 3)
    {
        usage(argv[0]);
        exit(1);
    }
    svrIp = argv[1];
    svrPort = atoi(argv[2]);

    int socketFd = socket(AF_INET, SOCK_DGRAM, 0);
    if (socketFd < 0)
    {
        std::cerr << "socket error" << std::endl;
        exit(2);
    }

    // 指向线程对象的智能指针
    std::unique_ptr<Thread> sender(new Thread(1, udpSender, (void *)&socketFd));
    std::unique_ptr<Thread> recver(new Thread(2, udpRecvr, (void *)&socketFd));

    sender->start();
    recver->start();

    sender->join();
    recver->join();

    close(socketFd);

    /* //客户端发给服务端 服务端回显给客户端
        struct sockaddr_in server;
        memset(&server, 0, sizeof(server));

        server.sin_family = AF_INET;
        server.sin_addr.s_addr = inet_addr(argv[1]);
        server.sin_port = htons(atoi(argv[2]));

        std::string msg;
        char buffer[1024];
        while (true)
        {
            std::cout << "请输入你的信息# ";
            std::getline(std::cin, msg);
            if (msg == "quit")
                break;

            // 当client首次发送消息给服务器的时候 OS会自动给client bind合适的IP和PORT
            sendto(socketFd, msg.c_str(), msg.size(), 0, (struct sockaddr *)&server, sizeof server);

            struct sockaddr_in sender_sockAddr;
            socklen_t len = sizeof(sender_sockAddr);
            ssize_t s = recvfrom(socketFd, buffer, sizeof buffer, 0, (struct sockaddr *)&sender_sockAddr, &len);
            if (s > 0)
            {
                buffer[s] = 0;
                std::cout << "server echo# " << std::endl
                          << buffer << std::endl;
            }
        }
        close(socketFd);
    */

    return 0;
}